package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;

import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.RecommendUser;

import com.itheima.service.db.CardService;
import com.itheima.service.db.UserInfoService;

import com.itheima.vo.CardVO;
import com.itheima.vo.LikeLoveHuxiang;
import com.itheima.vo.OMGVO;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CardManger {
    @DubboReference
    private CardService cardService;
    @DubboReference
    private UserInfoService userInfoService;

    //查询 推荐人的信息 mongo
    public ResponseEntity findCard() {
        //1获取线程id
        Long userId = UserHolder.get().getId();
        //2获取到好友的信息
        List<RecommendUser> recommendUserList = cardService.findrecommendUser(userId);
        //--------------
        ArrayList<CardVO> cardVOArrayList = new ArrayList<>();
        //3查看推荐人人的id
        if (CollectionUtil.isNotEmpty(recommendUserList)) {

            for (RecommendUser recommendUser : recommendUserList) {
                //4 查询
                UserInfo userInfo = userInfoService.findById(recommendUser.getUserId());
                //5封装
                CardVO vo = new CardVO();

                vo.setId(userInfo.getId().intValue()); //id
                vo.setAvatar(userInfo.getAvatar());  //图片

                vo.setNickname(userInfo.getNickname());  //名称
                vo.setGender(userInfo.getGender());  //性别
                vo.setAge(userInfo.getAge());  //照片
                String[] split = userInfo.getTags().split(",");
                vo.setTags(split);
                //6 集合
                cardVOArrayList.add(vo);
            }
        }
        return ResponseEntity.ok(cardVOArrayList);

    }
    //查询右滑喜欢
    public void rightLove(Integer likeUsrId) {
        //获取UserId
        Long userId = UserHolder.get().getId();
        cardService.rightLove(userId,likeUsrId);


    }
    //左滑不喜欢
    public void leftLove(Integer likeuserId) {
        Long touserId = UserHolder.get().getId();
        cardService.leftLove(touserId, likeuserId);

    }
    //查询 喜欢 互相喜欢 粉丝
    public ResponseEntity findlovelikefen() {
        //获取userid
        Long UserId = UserHolder.get().getId();
        //查询 我喜欢的 人
        OMGVO findlike = cardService.findlike(UserId);

        return ResponseEntity.ok(findlike);


    }

    //根据类型查询
    public ResponseEntity findlovelikefenUserInfo(Integer pageNumber, Integer pageSize, Integer type) {

        //获取用户id
        Long userId = UserHolder.get().getId();
        //获取pagebean
        PageBeanVo pageBeanVo = cardService.findlovelikefenUserInfo(pageNumber, pageSize, type, userId);


        List<Long> huxiang = cardService.huxiang(userId);
        //获取集合
        List<Long> userIdList = (List<Long>) pageBeanVo.getItems();
        //声明集合
        List<UserInfo> userInfoList = new ArrayList<>();
        //遍历
        if (CollectionUtil.isNotEmpty(userIdList)) {

            for (Long aLong : userIdList) {
                UserInfo userInfo = userInfoService.findById(aLong);
                //封装
                userInfoList.add(userInfo);
            }
        }
        //声明
        List<LikeLoveHuxiang> likeLoveHuxiangs = new ArrayList<>();

        //判空
        if (CollectionUtil.isNotEmpty(userInfoList)) {
            for (UserInfo userInfo : userInfoList) {
                //封装
                LikeLoveHuxiang likeLoveHuxiang = new LikeLoveHuxiang();
                likeLoveHuxiang.setId(userInfo.getId().intValue());  //id
                likeLoveHuxiang.setAvatar(userInfo.getAvatar()); //头像
                likeLoveHuxiang.setNickname(userInfo.getNickname()); //昵称
                likeLoveHuxiang.setGender(userInfo.getGender()); //性别
                likeLoveHuxiang.setAge(userInfo.getAge()); //年龄
                likeLoveHuxiang.setCity(userInfo.getCity());
                likeLoveHuxiang.setEducation(userInfo.getEducation());
                likeLoveHuxiang.setMarriage(userInfo.getMarriage());
                likeLoveHuxiang.setAlreadyLove(false);
                //如果是 3 喜欢我的 就和 互相喜欢的判断
                //--------------还有一种 将 互相喜欢的存储进入redis 中 判断我喜欢的人中 是不是互相喜欢
                if (type == 3) {
                    for (Long aLong : huxiang) {
                        if (aLong == userInfo.getId()) {

                            likeLoveHuxiang.setAlreadyLove(true);
                            break;
                        }
                        if (aLong != userInfo.getId()) {
                            likeLoveHuxiang.setAlreadyLove(false);
                        }


                    }

                }
                if (type == 4) {

                    List<Double> sore = (List<Double>) pageBeanVo.getSore();
                    for (Double aDouble : sore) {
                        likeLoveHuxiang.setMatchRate(aDouble.intValue());
                        sore.remove(aDouble);
                        break;

                    }
                }

                likeLoveHuxiangs.add(likeLoveHuxiang);
            }


            }


       pageBeanVo.setItems(likeLoveHuxiangs);
        return ResponseEntity.ok(pageBeanVo);

    }
    //喜欢的人
    public void findLove(Long Likeid) {
        //获取线程id
        Long userid = UserHolder.get().getId();

        cardService.findLove(Likeid,userid);
        huanXinTemplate.addContacts("HX" + userid, "HX" + Likeid);

    }
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    //取消喜欢
    public void deleteLove(Long likeId) {
        Long userId = UserHolder.get().getId();

        cardService.deleteLove(userId, likeId);

        huanXinTemplate.deleteContacts("HX" + userId, "HX"+likeId);


    }
}
